package nciae.db.controller;

import nciae.db.common.CommonResult;
import nciae.db.entity.Device;
import nciae.db.entity.User;
import nciae.db.service.DeviceService;
import nciae.db.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public CommonResult add(Device device){
        return deviceService.add(device);
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public CommonResult edit(Device device){
        return deviceService.edit(device);
    }

    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public CommonResult search(String name, Integer page, Integer size){
        return deviceService.listByPage(name,page,size);
    }

    @RequestMapping(value = "/get",method = RequestMethod.POST)
    public CommonResult getDeviceById(Integer id){
        CommonResult rs = new CommonResult();
        try{
            Device device = deviceService.getDeviceById(id);
            rs.setCode(0);
            rs.setMsg("获取设备成功");
            rs.setData(device);
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public CommonResult delete(Integer id){
        return deviceService.del(id);
    }

}
